<?php
class Capacitation extends AppModel {
	var $name = 'Capacitation';
	var $validate = array(
		'descripcion' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'person_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'presupuesto_asignado' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'fecha_inicio' => array(
			'date' => array(
				'rule' => array('date'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'horas_acumuladas' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'budget_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'project_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'gasto_acumulado' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
	);
	//The Associations below have been created with all possible keys, those that are not needed can be removed

	var $belongsTo = array(

		'Budget' => array(
			'className' => 'Budget',
			'foreignKey' => 'budget_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'Project' => array(
			'className' => 'Project',
			'foreignKey' => 'project_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
        'State' => array(
            'className' => 'State',
            /*'foreignKey' => 'company_id',
            'conditions' => '',
            'fields' => '',
            'order' => '' */
        )
	);

	var $hasMany = array(
		'Budget' => array(
			'className' => 'Budget',
			'foreignKey' => 'capacitation_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		),
		'Feedback' => array(
			'className' => 'Feedback',
			'foreignKey' => 'capacitation_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		),
		'Metric' => array(
			'className' => 'Metric',
			'foreignKey' => 'capacitation_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		),
		'SupplierHistory' => array(
			'className' => 'SupplierHistory',
			'foreignKey' => 'capacitation_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		),
		'Task' => array(
			'className' => 'Task',
			'foreignKey' => 'capacitation_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		)
	);


	var $hasAndBelongsToMany = array(
		'Person' => array(
			'className' => 'Person',
			'joinTable' => 'capacitation_participants',
			'foreignKey' => 'capacitation_id',
			'associationForeignKey' => 'person_id',
			'unique' => true,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'finderQuery' => '',
			'deleteQuery' => '',
			'insertQuery' => ''
		)        
	);
    
    /*function asignarResponsableCapacitacion($capacitationID, $personID = NULL)
    {
        $exists = $this->query("Select id from capacitations_people where capacitation_id = ".$capacitationID." and person_role_id = 3");
     
        if(isset($exists[0]['capacitations_people']['id']))
        {   

            if(!$personID)
            {
                $personID = 'NULL';
            }
            
            $this->query("UPDATE  capacitations_people SET person_id = ".$personID." where id = ".$exists[0]['capacitations_people']['id']);
        
        }else
        { 
             $id = $this->query("Select MAX(id) from capacitations_people");
             $regID = $id[0][0]['MAX(id)'] + 1;
            
             $this->query("INSERT INTO capacitations_people (id, capacitation_id, person_id, person_role_id) VALUES (".$regID.", ".$capacitationID.", ".$personID.", 3)");
        }

        
    } */
    
    function asignarResponsableCapacitacion($capacitationID, $personID = NULL)
    {
        $exists = $this->query("Select id from capacitation_responsibles where capacitation_id = ".$capacitationID);
     
        if(isset($exists[0]['capacitation_responsibles']['id']))
        {   

            if(!$personID)
            {
                $personID = 'NULL';
            }
            
            $this->query("UPDATE capacitation_responsibles SET person_id = ".$personID." where id = ".$exists[0]['capacitation_responsibles']['id']);
        
        }else
        { 
             $id = $this->query("Select MAX(id) from capacitation_responsibles");
             $regID = $id[0][0]['MAX(id)'] + 1;
            
             $this->query("INSERT INTO capacitation_responsibles (id, capacitation_id, person_id) VALUES (".$regID.", ".$capacitationID.", ".$personID.")");
        }

        
    }         
    
    
     function asignarResponsableLogistica($capacitationID, $personID = NULL)
    {
        $exists = $this->query("Select id from logistic_responsibles where capacitation_id = ".$capacitationID);
     
        if(isset($exists[0]['logistic_responsibles']['id']))
        {   

            if(!$personID)
            {
                $personID = 'NULL';
            }
            
            $this->query("UPDATE  logistic_responsibles SET person_id = ".$personID." where id = ".$exists[0]['logistic_responsibles']['id']);
        
        }else
        { 
             $id = $this->query("Select MAX(id) from logistic_responsibles");
             $regID = $id[0][0]['MAX(id)'] + 1;
            
             $this->query("INSERT INTO logistic_responsibles (id, capacitation_id, person_id) VALUES (".$regID.", ".$capacitationID.", ".$personID.")");
        }

        
    } 
    
    function asignarResponsableParticipantes($capacitationID, $personID = NULL)
    {
        $exists = $this->query("Select id from participant_responsibles where capacitation_id = ".$capacitationID);
     
        if(isset($exists[0]['participant_responsibles']['id']))
        {   

            if(!$personID)
            {
                $personID = 'NULL';
            }
            
            $this->query("UPDATE  participant_responsibles SET person_id = ".$personID." where id = ".$exists[0]['participant_responsibles']['id']);
        
        }else
        { 
             $id = $this->query("Select MAX(id) from participant_responsibles");
             $regID = $id[0][0]['MAX(id)'] + 1;
            
             $this->query("INSERT INTO participant_responsibles (id, capacitation_id, person_id) VALUES (".$regID.", ".$capacitationID.", ".$personID.")");
        }

        
    } 

}
?>